<?php
namespace App\Services;
use GuzzleHttp\Client;
use App\Models\Officialusersinfo;
use Illuminate\Support\Facades\Storage;
use EasyWeChat\Factory;
use App\Services\WechatopenService;
use Illuminate\Support\Facades\Redis;
use Log,Exception,Config;
use Symfony\Component\Cache\Adapter\RedisAdapter;

class MiniappdataService
{
    // 获取用户访问小程序数据日趋势
    public static function getDailyVisitTrend($access_token)
    {
        try {
            $begin_date = date("Ymd",strtotime("-1 days"));
            $end_date = date("Ymd",strtotime("-1 days"));
            $client = new Client();
            $response = $client->request('POST', 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token='.$access_token, [
                'json' => ['begin_date'=>$begin_date,'end_date'=>$end_date]
            ]);
            $resbody = $response->getBody();
            $resInfo = json_decode($resbody,true);
            if (empty($resInfo["list"])) {
                return ["session_cnt"=>0,"visit_pv"=>0,"visit_uv"=>0,"visit_uv_new"=>0,"stay_time_uv"=>0,"stay_time_session"=>0,"number"=>0];
            }
            return $resInfo["list"][0];
        } catch (Exception $e) {
            return ["session_cnt"=>0,"visit_pv"=>0,"visit_uv"=>0,"visit_uv_new"=>0,"stay_time_uv"=>0,"stay_time_session"=>0,"number"=>0];
        }
    }

    // 获取用户访问小程序数据概况
    public static function getDailySummary($access_token)
    {
        try {
            $begin_date = date("Ymd",strtotime("-2 days"));
            $end_date = date("Ymd",strtotime("-1 days"));
            $client = new Client();
            $response = $client->request('POST', 'https://api.weixin.qq.com/datacube/getweanalysisappiddailysummarytrend?access_token='.$access_token, [
                'json' => ['begin_date'=>$begin_date,'end_date'=>$end_date]
            ]);
            $resbody = $response->getBody();
            $resInfo = json_decode($resbody,true);
            dump($resInfo);
            if (empty($resInfo["list"])) {
                return ["session_cnt"=>0,"visit_pv"=>0,"visit_uv"=>0,"visit_uv_new"=>0,"stay_time_uv"=>0,"stay_time_session"=>0,"number"=>0];
            }
            return $resInfo["list"][0];
        } catch (Exception $e) {
            return ["session_cnt"=>0,"visit_pv"=>0,"visit_uv"=>0,"visit_uv_new"=>0,"stay_time_uv"=>0,"stay_time_session"=>0,"number"=>0];
        }
    }

    // 获取用户访问小程序数据周趋势
    public static function getWeeklyVisitTrend($access_token)
    {
        try {
            $begin_date = date('Ymd',strtotime('this week'));
            $end_date = date('Ymd',strtotime('next week'));
            $client = new Client();
            $response = $client->request('POST', 'https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token='.$access_token, [
                'json' => ['begin_date'=>$begin_date,'end_date'=>$end_date]
            ]);
            $resbody = $response->getBody();
            $resInfo = json_decode($resbody,true);
            if (empty($resInfo["list"])) {
                return ["session_cnt"=>0,"visit_pv"=>0,"visit_uv"=>0,"visit_uv_new"=>0,"stay_time_uv"=>0,"stay_time_session"=>0,"number"=>0];
            }
            return $resInfo["list"][0];
        } catch (Exception $e) {
            return ["session_cnt"=>0,"visit_pv"=>0,"visit_uv"=>0,"visit_uv_new"=>0,"stay_time_uv"=>0,"stay_time_session"=>0,"number"=>0];
        }
    }

    // 获取用户访问小程序数据月趋势
    public static function getMonthlyVisitTrend($access_token)
    {
        try {
            $begin_date = date('Ym01', strtotime($date));
            $end_date = date('Ymd', strtotime("$firstDay +1 month -1 day"));
            $client = new Client();
            $response = $client->request('POST', 'https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend?access_token='.$access_token, [
                'json' => ['begin_date'=>$begin_date,'end_date'=>$end_date]
            ]);
            $resbody = $response->getBody();
            $resInfo = json_decode($resbody,true);
            if (empty($resInfo["list"])) {
                return ["session_cnt"=>0,"visit_pv"=>0,"visit_uv"=>0,"visit_uv_new"=>0,"stay_time_uv"=>0,"stay_time_session"=>0,"number"=>0];
            }
            return $resInfo["list"][0];
        } catch (Exception $e) {
            return ["session_cnt"=>0,"visit_pv"=>0,"visit_uv"=>0,"visit_uv_new"=>0,"stay_time_uv"=>0,"stay_time_session"=>0,"number"=>0];
        }
    }
}
